Trò chơi game bắn súng đơn giản trong UNITY Engine
23.631 lượt xem;
1 using UnityEngine;
2 using System.Collections;
3
4 public class PlayerControl : MonoBehaviour, IInterractWithBullet
5 {
6 SoundManager sounds;
7 Rigidbody rb;
8 Pools pools;
9 UIgame ui;
10 MeshRenderer mesh;
11
12 public static int maxBulletsOnScreen;
13 static readonly float speedMaximum = 15;
14 static float speedCurrent;
15 static readonly int lifeMaximum = 4;
16 static int lifeCurrent;
17
18
19 //immortal-state-when-hit fields
20 bool immortal;
21 float immortalTime = 0.2f;
22
23 void Awake()
24 {
25 rb = GetComponent<Rigidbody>();
26 pools = GameObject.FindObjectOfType<Pools>();
27 ui = GameObject.FindObjectOfType<UIgame>();
28 sounds = GameObject.FindObjectOfType<SoundManager>();
29 mesh = GetComponent<MeshRenderer>();
30 }
31
32 void Start ()
33 {
34 maxBulletsOnScreen = 1;
35 lifeCurrent = lifeMaximum;
36 speedCurrent = 7;
37 }
38
39 void Update()
40 {
41 ShootOrPauseControls();
42 }
43
44 void FixedUpdate()
45 {
46 MovementControls();
47 }
48
49 void ShootOrPauseControls()
50 {
51 if (!SystemScr.paused)
52 {
53 if (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space))
54 {
55 GameObject p_bullet = pools.GetPoolableObject("p_bullet");
56
57 if (p_bullet != null && maxBulletsOnScreen > 0)
58 {
59 sounds.PlaySoundsPlayer(0);
60 p_bullet.SetActive(true);
61 p_bullet.GetComponent<PlayerBullet>().ShootMe(transform.position);
62 }
63 }
64 }
65 if (Input.GetKeyDown(KeyCode.Escape))
66 {
67 ui.Pause_btn();
68 }
69 }
70
71 void MovementControls()
72 {
73 float screenSpeed = 0;
74 float horizontalInput = 0;
75 float verticalInput = 0;
76
77 screenSpeed = CameraMotor.speedScreen;
78
79 //moving right
80 if (Input.GetKey(KeyCode.D))
81 horizontalInput = speedCurrent;
82
83 //moving left
84 if (Input.GetKey(KeyCode.A))
85 horizontalInput = -speedCurrent;
86
87 //moving up
88 if (Input.GetKey(KeyCode.W))
89 verticalInput = speedCurrent;
90
91 //moving down
92 if (Input.GetKey(KeyCode.S))
93 verticalInput = -speedCurrent;
94
95
96 //result velocity
97 rb.velocity = new Vector3(horizontalInput, 0, screenSpeed + verticalInput);
98 }
99
100 void OnTriggerEnter(Collider col)
101 {
102 if (col.tag == "Enemy" || col.tag == "Danger")
103 {
104 GetDamage();
105 }
106 if (col.tag == "Bonus")
107 {
108 sounds.PlaySoundsPlayer(3);
109 GetBonusEffect(col.GetComponent<Bonus>().thisBonus);
110 ui.ShowLives(lifeCurrent);
111 col.gameObject.SetActive(false);
112 }
113 }
114
115 public void GetDamage(int damage = 1)
116 {
117 if (immortal)
118 return;
119
120 immortal = true;
121
122 lifeCurrent-= damage;
123
124 ui.ShowLives(lifeCurrent);
125
126 if (lifeCurrent > 0)
127 {
128 sounds.PlaySoundsPlayer(1);
129 StartCoroutine("Immortality");
130 }
131 else
132 {
133 sounds.PlaySoundsPlayer(2);
134 Die();
135 }
136 }
137
138 public void Die()
139 {
140 StopCoroutine("Immortality");
141
142 immortal = false;
143
144 transform.gameObject.SetActive(false);
145 }
146
147
148 void GetBonusEffect(BonusesEnum currentBonus)
149 {
150 switch (currentBonus)
151 {
152 case (BonusesEnum.bonusMoney): IncreaseScoreByBonus(); break;
153 case (BonusesEnum.bonusBullet): IncreaseBulletsByBonus(); break;
154 case (BonusesEnum.bonusHealth): IncreaseCurrentLifeByBonus(); break;
155 case (BonusesEnum.bonusSpeed): IncreaseSpeedByBonus(); break;
156 }
157
158 }
159
160 public void IncreaseScoreByBonus(int score = 100)
161 {
162 ui.IncreaseScore(score);
163 }
164
165 public void IncreaseBulletsByBonus(int bullets = 1)
166 {
167 maxBulletsOnScreen += bullets;
168 ui.ShowInformation("+BULLET");
169 }
170
171 public void IncreaseCurrentLifeByBonus(int addLife = 1)
172 {
173 lifeCurrent += addLife;
174 if (lifeCurrent >= lifeMaximum)
175 {
176 lifeCurrent = lifeMaximum;
177 ui.ShowInformation("LIFE MAX");
178 }
179 else
180 ui.ShowInformation("+LIFE");
181 }
182
183 public void IncreaseSpeedByBonus(int addSpeed = 1)
184 {
185 speedCurrent += addSpeed;
186 if (speedCurrent >= speedMaximum)
187 {
188 speedCurrent = speedMaximum;
189 ui.ShowInformation("SPEED MAX");
190 }
191 else
192 ui.ShowInformation("+SPEED");
193 }
194
195
196 IEnumerator Immortality()
197 {
198 for (int i = 0; i < 8; i++)
199 {
200 mesh.enabled = !mesh.enabled;
201 yield return new WaitForSeconds(immortalTime);
202 }
203 mesh.enabled = true;
204 immortal = false;
205 }
206 }
2 using System.Collections;
3
4 public class PlayerControl : MonoBehaviour, IInterractWithBullet
5 {
6 SoundManager sounds;
7 Rigidbody rb;
8 Pools pools;
9 UIgame ui;
10 MeshRenderer mesh;
11
12 public static int maxBulletsOnScreen;
13 static readonly float speedMaximum = 15;
14 static float speedCurrent;
15 static readonly int lifeMaximum = 4;
16 static int lifeCurrent;
17
18
19 //immortal-state-when-hit fields
20 bool immortal;
21 float immortalTime = 0.2f;
22
23 void Awake()
24 {
25 rb = GetComponent<Rigidbody>();
26 pools = GameObject.FindObjectOfType<Pools>();
27 ui = GameObject.FindObjectOfType<UIgame>();
28 sounds = GameObject.FindObjectOfType<SoundManager>();
29 mesh = GetComponent<MeshRenderer>();
30 }
31
32 void Start ()
33 {
34 maxBulletsOnScreen = 1;
35 lifeCurrent = lifeMaximum;
36 speedCurrent = 7;
37 }
38
39 void Update()
40 {
41 ShootOrPauseControls();
42 }
43
44 void FixedUpdate()
45 {
46 MovementControls();
47 }
48
49 void ShootOrPauseControls()
50 {
51 if (!SystemScr.paused)
52 {
53 if (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space))
54 {
55 GameObject p_bullet = pools.GetPoolableObject("p_bullet");
56
57 if (p_bullet != null && maxBulletsOnScreen > 0)
58 {
59 sounds.PlaySoundsPlayer(0);
60 p_bullet.SetActive(true);
61 p_bullet.GetComponent<PlayerBullet>().ShootMe(transform.position);
62 }
63 }
64 }
65 if (Input.GetKeyDown(KeyCode.Escape))
66 {
67 ui.Pause_btn();
68 }
69 }
70
71 void MovementControls()
72 {
73 float screenSpeed = 0;
74 float horizontalInput = 0;
75 float verticalInput = 0;
76
77 screenSpeed = CameraMotor.speedScreen;
78
79 //moving right
80 if (Input.GetKey(KeyCode.D))
81 horizontalInput = speedCurrent;
82
83 //moving left
84 if (Input.GetKey(KeyCode.A))
85 horizontalInput = -speedCurrent;
86
87 //moving up
88 if (Input.GetKey(KeyCode.W))
89 verticalInput = speedCurrent;
90
91 //moving down
92 if (Input.GetKey(KeyCode.S))
93 verticalInput = -speedCurrent;
94
95
96 //result velocity
97 rb.velocity = new Vector3(horizontalInput, 0, screenSpeed + verticalInput);
98 }
99
100 void OnTriggerEnter(Collider col)
101 {
102 if (col.tag == "Enemy" || col.tag == "Danger")
103 {
104 GetDamage();
105 }
106 if (col.tag == "Bonus")
107 {
108 sounds.PlaySoundsPlayer(3);
109 GetBonusEffect(col.GetComponent<Bonus>().thisBonus);
110 ui.ShowLives(lifeCurrent);
111 col.gameObject.SetActive(false);
112 }
113 }
114
115 public void GetDamage(int damage = 1)
116 {
117 if (immortal)
118 return;
119
120 immortal = true;
121
122 lifeCurrent-= damage;
123
124 ui.ShowLives(lifeCurrent);
125
126 if (lifeCurrent > 0)
127 {
128 sounds.PlaySoundsPlayer(1);
129 StartCoroutine("Immortality");
130 }
131 else
132 {
133 sounds.PlaySoundsPlayer(2);
134 Die();
135 }
136 }
137
138 public void Die()
139 {
140 StopCoroutine("Immortality");
141
142 immortal = false;
143
144 transform.gameObject.SetActive(false);
145 }
146
147
148 void GetBonusEffect(BonusesEnum currentBonus)
149 {
150 switch (currentBonus)
151 {
152 case (BonusesEnum.bonusMoney): IncreaseScoreByBonus(); break;
153 case (BonusesEnum.bonusBullet): IncreaseBulletsByBonus(); break;
154 case (BonusesEnum.bonusHealth): IncreaseCurrentLifeByBonus(); break;
155 case (BonusesEnum.bonusSpeed): IncreaseSpeedByBonus(); break;
156 }
157
158 }
159
160 public void IncreaseScoreByBonus(int score = 100)
161 {
162 ui.IncreaseScore(score);
163 }
164
165 public void IncreaseBulletsByBonus(int bullets = 1)
166 {
167 maxBulletsOnScreen += bullets;
168 ui.ShowInformation("+BULLET");
169 }
170
171 public void IncreaseCurrentLifeByBonus(int addLife = 1)
172 {
173 lifeCurrent += addLife;
174 if (lifeCurrent >= lifeMaximum)
175 {
176 lifeCurrent = lifeMaximum;
177 ui.ShowInformation("LIFE MAX");
178 }
179 else
180 ui.ShowInformation("+LIFE");
181 }
182
183 public void IncreaseSpeedByBonus(int addSpeed = 1)
184 {
185 speedCurrent += addSpeed;
186 if (speedCurrent >= speedMaximum)
187 {
188 speedCurrent = speedMaximum;
189 ui.ShowInformation("SPEED MAX");
190 }
191 else
192 ui.ShowInformation("+SPEED");
193 }
194
195
196 IEnumerator Immortality()
197 {
198 for (int i = 0; i < 8; i++)
199 {
200 mesh.enabled = !mesh.enabled;
201 yield return new WaitForSeconds(immortalTime);
202 }
203 mesh.enabled = true;
204 immortal = false;
205 }
206 }
immortal-state-when-hit fields
moving right
moving left
moving up
moving down
result velocity